package com.sk.weichat.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mikhaellopez.circularprogressbar.CircularProgressBar;
import com.sk.weichat.R;
import com.sk.weichat.audio.IMRecordController;
import com.sk.weichat.audio.RecordListener;
import com.sk.weichat.audio.RecordManager;
import com.sk.weichat.audio_x.VoicePlayer;
import com.sk.weichat.course.ChatRecordHelper;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.ui.message.MucChatActivity;
import com.sk.weichat.util.DisplayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2016/9/8.
 */
public class ChatVoiceView extends RelativeLayout {


    private Context mContext;
    private ImageView mImgVoiceLeft;
    private ImageView mImgVoiceRight;
    private IMRecordController mRecordController;
    private TextView mTvVoice;
    private Button mBtnVoice;
    private CircularProgressBar mCprogressVoice;
    private OnChatRecordListener mChatRecordListener;
    private int progrogress;//录音进度

    public ChatVoiceView(Context context) {
        super(context);
        init(context);
    }

    public ChatVoiceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ChatVoiceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setChatRecordListener(OnChatRecordListener listener) {
        mChatRecordListener = listener;
    }

    private void init(Context context) {
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.chat_voice_view, this);


        mImgVoiceLeft = findViewById(R.id.img_voice_left);
        mImgVoiceRight = findViewById(R.id.img_voice_right);
        mTvVoice = findViewById(R.id.tv_voice_tip);
        mBtnVoice = findViewById(R.id.btn_Voice);
        mCprogressVoice = findViewById(R.id.cprogress_voice);
        mRecordController = new IMRecordController(mContext);
        mRecordController.setRecordListener(new RecordListener() {
            @Override
            public void onRecordSuccess(String filePath, int timeLen) {
                // 录音成功，返回录音文件的路径
                mTvVoice.setText(R.string.motalk_voice_chat_tip_1);
                mBtnVoice.setBackgroundResource(R.drawable.btn_chat_voice_normal);
                progrogress = 0;
                mCprogressVoice.setProgress(progrogress);
                mCprogressVoice.setVisibility(GONE);
                setRecordVolumeChange(0);
                if (timeLen < 1) {
                    Toast.makeText(mContext, InternationalizationHelper.getString("JXChatVC_TimeLess"), Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mChatRecordListener != null) {
                    mChatRecordListener.onRecordSuccess(filePath, timeLen);
                }
            }


            @Override
            public void onRecordVolumeChange(int v) {
                setRecordVolumeChange(v);
            }

            @Override
            public void onUpMove() {
                if (!mTvVoice.getText().toString().equals(mContext.getResources().getString(R.string.motalk_voice_chat_tip_4))) {
                    mTvVoice.setText(R.string.motalk_voice_chat_tip_4);
                    mTvVoice.setTextColor(ContextCompat.getColor(mContext, R.color.color_FF2626));
                    mBtnVoice.setBackgroundResource(R.drawable.btn_chat_voice_del);
                }
            }

            @Override
            public void onCancelUpMove() {
                mTvVoice.setText(R.string.motalk_voice_chat_tip_2);
                mTvVoice.setTextColor(ContextCompat.getColor(mContext, R.color.color_929292));
                mBtnVoice.setBackgroundResource(R.drawable.btn_chat_voice_active);
            }

            @Override
            public void onUpOrCancel() {
                if (mTvVoice.getText().toString().equals(mContext.getResources().getString(R.string.motalk_voice_chat_tip_4))) {
                    RecordManager.getInstance().cancel();
                } else {
                    // ACTION_CANCEL 可能情况是这个录音已经stop, 再次调用stop会导致native崩溃，
                    if (RecordManager.getInstance().isRunning()) {
                        RecordManager.getInstance().stop();
                    }
                }
            }

            @Override
            public void onRecordTimeChange(int seconds) {
                Log.d("seconds", "时间:" + seconds);
                progrogress++;
                mCprogressVoice.setProgress(progrogress);
            }

            @Override
            public void onRecordStart() {
                mChatRecordListener.onRecordStart();
                // 录音开始
                mTvVoice.setText(R.string.motalk_voice_chat_tip_2);
                mBtnVoice.setBackgroundResource(R.drawable.btn_chat_voice_active);
                progrogress = 0;
                mCprogressVoice.setProgress(progrogress);
                mCprogressVoice.setVisibility(VISIBLE);

            }

            @Override
            public void onRecordCancel() {
                // 录音取消
                mTvVoice.setText(R.string.motalk_voice_chat_tip_1);
                setRecordVolumeChange(0);
                mBtnVoice.setBackgroundResource(R.drawable.btn_chat_voice_normal);
                mTvVoice.setTextColor(ContextCompat.getColor(mContext, R.color.color_929292));
                progrogress = 0;
                mCprogressVoice.setProgressWithAnimation(progrogress);
                mCprogressVoice.setVisibility(GONE);

            }

        });
        mBtnVoice.setOnTouchListener(mRecordController);
    }


    public void setRecordVolumeChange(int v) {
        int level = v / 1000;
        Log.d("roamer", "level1:" + level);
        if (level < 1) {
            level = 0;
            Log.d("roamer", "level2:" + level);
        } else if (level > 12) {
            level = 12;
            Log.d("roamer", "level2:" + level);
        }
        int resId = mContext.getResources().getIdentifier("voice" + level, "drawable", mContext.getPackageName());
        mImgVoiceLeft.setImageResource(resId);
        mImgVoiceRight.setImageResource(resId);
    }

    public void cancel() {
        if (mRecordController != null) {
            mRecordController.cancel();
        }
    }

    public interface OnChatRecordListener {
        void onRecordStart();
        void onRecordSuccess(String filePath, int timeLen);
    }
}
